# -*- coding: utf-8 -*-
import scrapy
import re
from ..items import LjItem


class RentingSpider(scrapy.Spider):
    name = 'renting'
    allowed_domains = ['sz.lianjia.com']
    start_urls = ['https://sz.lianjia.com/zufang/rt200600000001/']

    # 重写start_request
    def start_requests(self):
        base_url = "https://sz.lianjia.com/zufang/"
        for i in range(1,100):
            url = base_url + 'pg' + str(i) + "rt200600000001/"
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):

        # 当前页面的所有租房信息
        house_list = response.xpath("//div[@class='content__list--item']")
        for house in house_list:
            # 租房名字
            name = house.xpath(".//div/p[1]/a/text()").extract_first().strip()
            # 房屋描述
            contents = house.xpath(".//div/p[2]//text()")
            content = ''
            for cont in contents:
                # print(cont.get().strip())
                content += cont.get().replace(" ","").replace('\n','')  # 去除空格并拼接
            # 价格
            prices = house.xpath(".//div/span//text()")
            price = ''
            for pric in prices:
                price += pric.get().strip()
            # 详情页面url
            next_url = 'https://sz.lianjia.com' + house.xpath(".//div/p[1]/a/@href").get()
            request = scrapy.Request(url=next_url, callback=self.next_parse)
            request.meta['name'] = name
            request.meta['content'] = content
            request.meta['price'] = price
            yield request

    def next_parse(self, response):
        name = response.meta['name']         # 租房名字
        content = response.meta['content']   # 简介
        price = response.meta['price']       # 价格

        # 坐标,使用正则匹配
        # 经度
        longitude, = re.findall("longitude: \'(\d+\.\d+)\'", response.text)
        # 纬度
        latitude, = re.findall("latitude: \'(\d+\.\d+)\'", response.text)
        # print('经度',longitude)
        # print('纬度',latitude)

        ljitem = LjItem()
        ljitem['name'] = name
        ljitem['content'] = content
        ljitem['price'] = price
        ljitem['longitude'] = longitude
        ljitem['latitude'] = latitude
        print(name,'爬去结束')
        yield ljitem
